package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.model.common.admin.dto.AdminDictDataDto;
import com.zhentao.model.common.admin.pojo.AdminUser;
import com.zhentao.model.common.admin.pojo.DictData;
import com.zhentao.model.common.admin.pojo.DictType;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.DictDataService;
import com.zhentao.mapper.DictDataMapper;
import com.zhentao.service.DictTypeService;
import com.zhentao.util.UserThreadLocalutil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author liu
* @description 针对表【dict_data】的数据库操作Service实现
* @createDate 2024-09-27 10:36:10
*/
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData>
    implements DictDataService{
    @Resource
    private DictDataMapper dictDataMapper;
    @Resource
    private DictTypeService dictTypeService;

    /**
     * 根据字典类型查询该类型下的数据
     * @param
     * @return
     */
    @Override
    public ResponseResult adminDictDataList(AdminDictDataDto adminDictDataDto) {
        PageHelper.startPage(adminDictDataDto.getPageNum(),adminDictDataDto.getPageSize());
        List<DictData> dictData = dictDataMapper.selectList(Wrappers.<DictData>lambdaQuery()
                //根据字典类型查
                .eq(StringUtils.isNotBlank(adminDictDataDto.getDictType()), DictData::getDictType, adminDictDataDto.getDictType())
                //根据字典标签查
                .eq(StringUtils.isNotBlank(adminDictDataDto.getDictLabel()), DictData::getDictLabel, adminDictDataDto.getDictLabel())
                //根据状态查
                .eq(adminDictDataDto.getStatus() != 0, DictData::getStatus, adminDictDataDto.getStatus())
        );
        PageInfo<DictData> dictDataPageInfo = new PageInfo<>(dictData);


        return ResponseResult.okResult(dictDataPageInfo);
    }

    /**
     * 根据字典类型查询
     * @param dictType
     * @return
     */
    @Override
    public ResponseResult adminDictDataSel(String dictType) {
        List<DictData> dictData = dictDataMapper.selectList(Wrappers.<DictData>lambdaQuery()
                //根据字典类型查
                .eq(StringUtils.isNotBlank(dictType), DictData::getDictType, dictType)
        );
        return ResponseResult.okResult(dictData);
    }

    /**
     * 字典数据表---添加-修改-删除
     * @param dictData
     * @return
     */
    @Override
    public ResponseResult adminCURDDictData(DictData dictData) {
        if (StringUtils.isNotBlank(dictData.getDictType())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"缺失字典类型");
        }
        if (StringUtils.isNotBlank(dictData.getDictLabel())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"标签不能为空");
        }
        if (StringUtils.isNotBlank(dictData.getDictValue())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"键值不能为空");
        }

        //判断标签和键值是否重复
        DictData dictData1 = dictDataMapper.selectOne(Wrappers.<DictData>lambdaQuery()
                .eq(DictData::getDictType, dictData.getDictType())
                .eq(DictData::getDictLabel, dictData.getDictLabel()));
        if (dictData1!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"标签已存在");
        }
        DictData dictData2 = dictDataMapper.selectOne(Wrappers.<DictData>lambdaQuery()
                .eq(DictData::getDictType, dictData.getDictType())
                .eq(DictData::getDictValue, dictData.getDictValue()));
        if (dictData2!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"键值已存在");
        }

        AdminUser user = UserThreadLocalutil.getUser();
        String username = user.getUsername();

        if (dictData.getButCode()==1){
            //进行添加
            dictData.setCreateBy(username);
            dictData.setCreateTime(new Date());
            dictDataMapper.insert(dictData);
            return ResponseResult.okResult("添加成功");
        }else if (dictData.getButCode()==2){
            dictData.setUpdateBy(username);
            dictData.setUpdateTime(new Date());
            dictDataMapper.updateById(dictData);
            return ResponseResult.okResult("修改成功");
        }else {
            dictDataMapper.deleteById(dictData.getDictCode());
            return ResponseResult.okResult("删除成功");
        }

    }

    @Override
    public ResponseResult adminDictDataById(Integer dictDataId) {
        DictData dictData = dictDataMapper.selectById(dictDataId);
        String dictType = dictData.getDictType();
        //通过字典类型查询字典名称
        DictType dictType1 = dictTypeService.getOne(Wrappers.<DictType>lambdaQuery()
                .eq(DictType::getDictType, dictType));
        Integer dictId = dictType1.getDictId();
        return ResponseResult.okResult(dictId);
    }
}




